<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>获取选择</title>
	</head>
	<body>
		<p id="target">我是一段内容<b>我是B标签</b></p>
		<br />
		<p id="cloned"></p>
		<button>克隆选择</button>

		<script>
			const cloned = document.querySelector('#target')

			document.querySelector('button').addEventListener('click', () => {
				const selection = document.getSelection()
				// 然后将 Range 方法应用于 selection.getRangeAt(0)
				// 或者，像这样，用于所有范围，以支持多选
				for (let i = 0; i < selection.rangeCount; i++) {
					cloned.append(selection.getRangeAt(i).cloneContents())
				}
			})
		</script>
	</body>
</html>
